package cn.stylefeng.guns.modular.business.frequency.cmcompanyline.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.modular.business.frequency.cmcompanyline.entity.CmCompanyLine;
import cn.stylefeng.guns.modular.business.frequency.cmcompanyline.mapper.CmCompanyLineMapper;
import cn.stylefeng.guns.modular.business.frequency.cmcompanyline.model.param.CmCompanyLineParam;
import cn.stylefeng.guns.modular.business.frequency.cmcompanyline.service.CmCompanyLineService;
import cn.stylefeng.guns.modular.business.frequency.cmcompanyline.enums.CmCompanyLineExceptionEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 客户线路表 服务实现类
 *
 * @author yujie
 * @date 2023/05/09 17:17
 */
@Service
public class CmCompanyLineServiceImpl extends ServiceImpl<CmCompanyLineMapper, CmCompanyLine> implements CmCompanyLineService {

    @Override
    public PageResult<CmCompanyLine> page(CmCompanyLineParam cmCompanyLineParam) {

        // 构造条件
        LambdaQueryWrapper<CmCompanyLine> queryWrapper = new LambdaQueryWrapper<>();

        // 查询分页结果
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<CmCompanyLine> list(CmCompanyLineParam cmCompanyLineParam) {

        // 构造条件
        LambdaQueryWrapper<CmCompanyLine> queryWrapper = new LambdaQueryWrapper<>();

        return this.list(queryWrapper);
    }

    @Override
    public void add(CmCompanyLineParam cmCompanyLineParam) {

        // 将dto转为实体
        CmCompanyLine cmCompanyLine = new CmCompanyLine();
        BeanUtil.copyProperties(cmCompanyLineParam, cmCompanyLine);

        this.save(cmCompanyLine);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(CmCompanyLineParam cmCompanyLineParam) {
        this.removeById(cmCompanyLineParam.getId());
    }

    @Override
    public void edit(CmCompanyLineParam cmCompanyLineParam) {

        // 根据id查询实体
        CmCompanyLine cmCompanyLine = this.queryCmCompanyLine(cmCompanyLineParam);

        // 请求参数转化为实体
        BeanUtil.copyProperties(cmCompanyLineParam, cmCompanyLine);

        this.updateById(cmCompanyLine);
    }

    @Override
    public CmCompanyLine detail(CmCompanyLineParam cmCompanyLineParam) {
        return this.queryCmCompanyLine(cmCompanyLineParam);
    }



    /**
     * 获取客户线路表
     *
     * @author yujie
     * @since 2023/05/09 17:17
     */
    private CmCompanyLine queryCmCompanyLine(CmCompanyLineParam cmCompanyLineParam) {
        CmCompanyLine cmCompanyLine = this.getById(cmCompanyLineParam.getId());
        if (ObjectUtil.isEmpty(cmCompanyLine)) {
            throw new ServiceException(CmCompanyLineExceptionEnum.NOT_EXISTED);
        }
        return cmCompanyLine;
    }
}